package com.taylor.wc;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @Desc : the first flink demo 批处理执行环境
 * @Author ： taylor
 * @Date ：2022/1/25 14:23
 */
public class WorldCount {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
        DataSet<String> dataSet = environment.readTextFile("D:\\IdeaProjects\\flink\\Flinkstudy\\src\\main\\resources\\test.txt");

        DataSet<Tuple2<String, Integer>> resultSet = dataSet.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String s, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = s.split(" ");
                for (String word : words) {
                    out.collect(new Tuple2<String, Integer>(word, 1));
                }
            }
        }).groupBy(0).sum(1);


        resultSet.print();
    }
}
